<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class diversos extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
    function __construct(){
        parent::__construct();

        $this->load->helper("auth");
        $this->load->model("ProdutoModel");
    }
    public function index($mensagem = "")
    {
        if ($mensagem == "sucesso") $mensagem = "Operação realizada com sucesso";
        else $mensagem = "";
        $ofertas = $this->ProdutoModel->listarOfertas();

        $main = array(
        "dados" => "",
        "link" => "home_view",
        "mensagem" => $mensagem
        );
        $this->load->view('template',$main);
    }

    public function fale_conosco(){

        $this->load->helper("form");
        $this->load->library("form_validation");

        //INICIO CAPTCHA
        $this->load->helper('captcha');

        $vals = array(
            'img_path' => 'captcha/',
            'img_url' => base_url().'captcha/',
            'img_width' => 200,
            'img_height' => 60,
            'expiration' => 900
            );
        $cap = create_captcha($vals);

        $data = array(
            'captcha_time' => $cap['time'],
            'ip_address' => $this->input->ip_address(),
            'word' => $cap['word']
            );

        $query = $this->db->insert_string('captcha', $data);
        $this->db->query($query);
        // FIM CAPTCHA

        $dados = array(
            "titulo" => "Fale Conosco",
            "erro" => "",
            "cap" => $cap,
            "captcha_fail" => ""
            );

        $main = array(
            "dados" => $dados,
            "link" => "diversos/fale_conosco_view",
            );

        //Validação dos campos
        $this->form_validation->set_rules('nome', 'Nome', 'required|min_length[3]');;
        $this->form_validation->set_rules('email', 'E-mail', 'required|valid_email');
        $this->form_validation->set_rules('assunto', 'Assunto');

        if ($this->form_validation->run() == false) :
            $this->load->view('template', $main);

        else :
            $expiration = time()-900;
             // Then see if a captcha exists:
            $sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?";
            $binds = array($_POST['captcha'], $this->input->ip_address(), $expiration);
            $query = $this->db->query($sql, $binds);
            $row = $query->row();

            if ($row->count == 0){
                $dados["captcha_fail"] = "Você deve escrever os caracteres que aparecem na imagem";
                $main['dados'] = $dados;
                $this->load->view('template', $main);
            }
            //FIM VERIFICACAO CAPTCHA
            else{
                $campos = array(
                    "nome" => $this->input->post("nome"),
                    "email" => $this->input->post("email"),
                    "assunto" => $this->input->post("assunto"),
                    "mensagem" => $this->input->post("mensagem"),
                );

                $this->load->helper("mail");

                emailFaleConosco($campos);

                redirect("home/index/email_sucesso");
                }
        endif;
	}
            
        
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */